<template>
  <!-- 歌单详情 -->
  <div class="mod-pld">
    <div class="container" v-if="playlistCode == 200">
      <div class="pld-hd" v-if="playlistData != {}">
        <div class="pld-hd-pic">
          <img
            :src="playlistData.coverImgUrl"
            :alt="playlistData.name"
            class="pld-pic"
          />
        </div>
        <div class="pld-hd-main">
          <div class="pld-title-box">
            <h1 class="pld-title">
              <a href="javascript:;" :title="playlistData.name">{{
                playlistData.name
              }}</a>
            </h1>
          </div>

          <div class="pld-creator-box" v-if="playlistData.creator">
            <div class="pld-creator-left">
              <a
                href="javascript:;"
                @click="clickUser(playlistData.creator.userId)"
                :title="playlistData.creator.nickname"
              >
                <img
                  :src="playlistData.creator.avatarUrl"
                  :alt="playlistData.creator.nickname"
                  class="pld-creator-pic"
                />
              </a>
            </div>
            <h4 class="pld-creator">
              <a
                href="javascript:;"
                @click="clickUser(playlistData.creator.userId)"
                :title="playlistData.creator.nickname"
                >{{ playlistData.creator.nickname }}</a
              >
            </h4>
            <span class="pld-createTime"
              >{{ playlistData.createTime | showDate }} 创建</span
            >
          </div>

          <div
            class="pld-tags"
            v-if="playlistData.tags && playlistData.tags.length > 0"
          >
            标签：
            <span>
              <a
                href="javascript:;"
                v-for="tag in playlistData.tags"
                :key="tag"
                class="tag-item"
                @click="clickPlaylist(tag)"
              >
                {{ tag }}
              </a>
            </span>
          </div>

          <div class="mod-songlist-toolbar">
            <a class="mod-btn-blue">
              <span class="btn-play" @click="playAll">
                <i class="iconfont">&#xe8a8;</i>
                全部播放
              </span>
              <span class="btn-add">
                <i class="iconfont" @click="addAll">&#xe603;</i>
              </span>
            </a>
            <a class="mod-btn" @click="colPlaylist">
              <i class="iconfont">&#xe8c8;</i>
              收藏({{ playlistData.subscribedCount }})
            </a>
            <a class="mod-btn">
              <i class="iconfont">&#xe8a6;</i>
              下载
            </a>
            <a class="mod-btn" @click="clickShare">
              <i class="iconfont">&#xe8af;</i>
              分享({{ playlistData.shareCount }})
            </a>
            <a class="mod-btn">
              <i class="iconfont">&#xe8a7;</i>
              评论（{{ commentData.total }}）
            </a>
          </div>
        </div>
      </div>

      <div class="pld-relative-box">
        <h2 class="pld-relative-hd">介绍</h2>
        <pre :class="['pld-description', { limit: isLimit }]">{{
          playlistData.description
        }}</pre>
        <a class="pld-description-height" @click="changeLimit">{{ show }}</a>
      </div>
      <div class="pld-left">
        <div class="pld-list" v-if="showSongList">
          <div class="songlist-message">
            <h3>歌曲列表</h3>
            <span class="songlist-num">{{ playlistData.trackCount }}首歌</span>
            <span class="songlist-play"
              >播放：<span class="playcount">{{ playlistData.playCount }}</span
              >次</span
            >
          </div>
          <div v-if="showSong">
            <song-list
              :songs="songs[currentPageS - 1]"
              :p="currentPageS"
              ref="songlist"
            ></song-list>
          </div>
          <div v-if="!showSong" class="noSong">
            <p class="hd">暂无音乐</p>
            <p class="m">
              点击
              <i class="iconfont">&#xe8c8;</i>
              即可将你喜欢的音乐收藏到“我的音乐”，马上去
              <a href="javascript:;" @click="clickHome">发现音乐</a>
            </p>
          </div>
          <div
            class="songlist-el-pagination"
            v-if="playlistData.trackCount > 20"
          >
            <el-pagination
              background
              @current-change="handleCurrentChangeS"
              :current-page.sync="currentPageS"
              :page-size="20"
              layout="prev, pager, next, jumper"
              :total="playlistData.trackCount"
            >
            </el-pagination>
          </div>
        </div>
        <div class="pld-comment" v-if="commentData && showSong">
          <comment ref="comment" :commentData="commentData"></comment>
          <div class="comment-el-pagination" v-if="commentData.total > 20">
            <el-pagination
              background
              @current-change="handleCurrentChangeC"
              :current-page.sync="currentPageC"
              :page-size="20"
              layout="prev, pager, next, jumper"
              :total="commentData.total"
            >
            </el-pagination>
          </div>
        </div>
      </div>
      <div class="pld-relative">
        <div
          class="pld-relative-box"
          v-if="collectors && collectors.length > 0"
        >
          <h2 class="pld-relative-hd">歌单收藏者</h2>
          <ul class="pld-collector clearfix">
            <li
              class="pld-collector-item"
              v-for="item in collectors"
              :key="item.userId"
            >
              <a
                href="javascript:;"
                :title="item.nickname"
                @click="clickUser(item.userId)"
              >
                <img :src="item.avatarUrl" :alt="item.nickname" />
              </a>
              <!-- <h4 class="collector-name">
                <a href="javascript:;">{{item.nickname}}</a>
              </h4> -->
            </li>
          </ul>
        </div>

        <div class="pld-relative-box" v-if="resong.length > 0">
          <h2 class="pld-relative-hd">热门歌单推荐</h2>
          <div class="resong-box" v-for="item in resong" :key="item.id">
            <div class="left">
              <a href="javascript:;" @click="clickPlaylistDetail(item.id)">
                <img
                  :src="item.coverImgUrl"
                  :alt="item.name"
                  class="resong-pic"
                />
              </a>
            </div>
            <h4 class="resong-name">
              <a
                href="javascript:;"
                :title="item.name"
                @click="clickPlaylistDetail(item.id)"
                >{{ item.name }}</a
              >
            </h4>
            <h4 class="resong-creator">
              <a
                href="javascript:;"
                :title="item.creator.nickname"
                @click="clickUser(item.creator.userId)"
              >
                by
                {{ item.creator.nickname }}
              </a>
            </h4>
          </div>
        </div>
      </div>
    </div>
    <no-resource v-if="playlistCode == 404"></no-resource>
    <div class="not-logged" v-if="playlistCode == 20001">
      <span class="not-gif">
        <img :src="notLogged" alt="乾心寿" />
      </span>
      <span class="not-content">登录后才可查看</span>
    </div>

    <small-tip :tipMessage="smallTipMsg" v-if="showSmallTip"></small-tip>
  </div>
</template>

<script>
import { formatDate } from "common/date.js";
import SongList from "views/toplists/child/SongList.vue";
import Comment from "views/toplists/child/Comment.vue";
import notLogged from "assets/images/notLogged.gif";
import SmallTip from "components/tip/SmallTip";
import NoResource from "views/notfound/NoResource";

export default {
  components: {
    SongList,
    Comment,
    SmallTip,
    NoResource,
  },
  filters: {
    // 过滤器转换时间戳
    showDate(value) {
      const date = new Date(value);
      return formatDate(date, "yyyy-MM-dd");
    },
  },
  data() {
    return {
      playlistData: {},
      commentData: {},
      // 歌单列表,用来分组
      songs: [],
      showSong: true,
      // 总歌单
      allSong: [],
      // 相关歌单
      resong: [],
      currentPageS: 1,
      currentPageC: 1,
      // 歌单收藏者
      collectors: [],
      showSongList: false,
      // 控制简介高度
      isLimit: true,
      show: "[展开]",
      playlistCode: 200,
      notLogged: notLogged,
      showSmallTip: false,
      smallTipMsg: "操作提示",
      code: 200,
    };
  },
  mounted() {
    this.id = this.$route.query.id;
    this.getPlaylistDetail();
  },
  methods: {
    clickShare() {
      if (this.$store.state.isLogin) {
        let newItem = {
          id: this.playlistData.id,
          type: "playlist",
          name: this.playlistData.name,
          artist: this.playlistData.creator.nickname,
        };
        this.$store.commit("changeShowShare", true);
        this.$store.commit("changeShareItem", newItem);
      } else {
        this.smallTipMsg = "请登录后再分享歌单";
        this.showSmallTip = true;
        setTimeout(() => {
          this.showSmallTip = false;
        }, 1000);
      }
    },
    colPlaylist() {
      console.log(this.id);
      // 收藏歌单
      if (this.$store.state.isLogin) {
        // 已登录
        this.axios({
          url: "/playlist/subscribe?t=1&id=" + this.id,
        })
          .then((res) => {
            console.log(res);
            if (res.data.code == 200) {
              this.smallTipMsg = "收藏歌单成功！";
              this.showSmallTip = true;
              setTimeout(() => {
                this.showSmallTip = false;
              }, 1000);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.smallTipMsg = "请登录后再收藏歌单";
        this.showSmallTip = true;
        setTimeout(() => {
          this.showSmallTip = false;
        }, 1000);
      }
    },
    clickUser(id) {
      this.playUserDetail(id);
    },
    clickHome() {
      this.$router.push("/home");
    },
    playAll() {
      if (this.allSong.length != 0) {
        var newItem = [];
        this.allSong.forEach((item) => {
          newItem[newItem.length] = {
            id: item.id,
            name: item.name,
            ar: item.ar,
            al: item.al,
            mv: item.mv,
            dt: item.dt,
            fee: item.fee,
          };
        });
        this.playSongs(newItem, true);
      }
    },
    addAll() {
      if (this.allSong.length != 0) {
        var newItem = [];
        this.allSong.forEach((item) => {
          newItem[newItem.length] = {
            id: item.id,
            name: item.name,
            ar: item.ar,
            al: item.al,
            mv: item.mv,
            dt: item.dt,
            fee: item.fee,
          };
        });
        console.log(newItem);
        this.addSongs(newItem, true);
      }
    },
    clickUser(id) {
      this.playUserDetail(id);
    },
    clickPlaylist(cat) {
      this.playPlaylists(cat);
    },
    clickPlaylistDetail(id) {
      this.playPlaylistDetail(id);
    },
    changeLimit() {
      this.isLimit = !this.isLimit;
      if (this.isLimit) {
        this.show = "[展开]";
        document.documentElement.scrollTo({
          top: 141,
          behavior: "smooth",
        });
      } else {
        this.show = "[收起]";
      }
    },
    // getCollector() {
    //   // 可以从getPlaylistDetail()中获取8个收藏者信息
    //   this.axios({
    //     /* 歌单收藏者接口地址 */
    //     url:
    //       "/playlist/subscribers?id=7277029971&limit=6",
    //   })
    //     .then((res) => {
    //       this.collectors = res.data.subscribers;
    //     })
    //     .catch((err) => {
    //       console.log(err);
    //     });
    // },
    getRelatedPlaylist() {
      this.axios({
        /* 相关歌单推荐接口地址 */
        url: "/related/playlist?id=" + this.id,
      })
        .then((res) => {
          this.resong = res.data.playlists;
          // console.log(this.resong);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleCurrentChangeS() {
      // console.log(this.currentPage);
      // console.log(`当前页: ${val}`);
      // 获取歌单
      // console.log(this.currentPageS);
      // this.getSongList();
      // 点击分页后，滚动到评论的头部，141为页面头部组件的高度
      document.documentElement.scrollTo({
        top: this.$refs.songlist.$el.offsetTop + 141,
        behavior: "smooth",
      });
    },
    getSongList() {
      this.axios({
        /* 歌曲列表接口地址 */
        url: "/playlist/track/all?id=" + this.id,
      })
        .then((res) => {
          this.allSong = res.data.songs;
          if (res.data.songs.length == 0) {
            this.showSong = false;
          } else {
            this.showSong = true;
          }
          // 每20收歌分一组，总组数：sl
          let sl = Math.ceil(res.data.songs.length / 20);
          for (let i = 0; i < sl; i++) {
            this.songs[i] = res.data.songs.slice(i * 20, (i + 1) * 20);
          }
          this.showSongList = true;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 6947604785
    handleCurrentChangeC() {
      // console.log(this.currentPage);
      // console.log(`当前页: ${val}`);
      this.getComment();
      // 点击分页后，滚动到评论的头部，141为页面头部组件的高度
      document.documentElement.scrollTo({
        top: this.$refs.comment.$el.offsetTop + 141,
        behavior: "smooth",
      });
    },
    getComment() {
      this.axios({
        /* 歌单评论接口地址 */
        url:
          "/comment/playlist?id=" +
          this.id +
          "&offset=" +
          (this.currentPageC - 1) * 20,
      })
        .then((res) => {
          this.commentData = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getPlaylistDetail() {
      this.axios({
        /* 歌单详情 */
        url: "/playlist/detail?id=" + this.id,
      })
        .then((res) => {
          // console.log(res.data);
          if (res.data && res.data.code == 200) {
            this.playlistCode = 200;
            this.playlistData = res.data.playlist;
            this.collectors = res.data.playlist.subscribers;
            this.getComment();
            this.getSongList();
            this.getRelatedPlaylist();
            // console.log("200");
          } else if (res.data && res.data.code == 20001) {
            this.playlistCode = 20001;
          } else {
            this.playlistCode = 404;
            // console.log("20001");
          }
        })
        .catch((err) => {
          console.log(err);
          // this.playlistCode = 404;
        });
    },
  },
};
</script>

<style>
.mod-pld {
  padding-top: 50px;
  background-color: var(--color-theme-light-5);
}
.pld-hd {
  position: relative;
  height: 250px;
  margin-bottom: 50px;
}
.pld-hd-pic {
  position: absolute;
  left: 0;
  top: 0;
  width: 250px;
  height: 250px;
}
.pld-pic {
  width: 100%;
  height: 100%;
}
/* pld-hd-main */
.pld-hd-main {
  position: relative;
  margin-left: 300px;
  height: 250px;
  overflow: hidden;
}
.pld-title,
.pld-creator {
  font-weight: 400;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.pld-title-box,
.pld-creator-box {
  height: 50px;
  line-height: 50px;
}
.pld-creator-box {
  position: relative;
  margin: 15px 0 15px;
  overflow: hidden;
}
.pld-creator-left {
  float: left;
  height: 50px;
  width: 50px;
}
.pld-creator-left a {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 50px;
}
.pld-creator-pic {
  height: 50px;
  width: 50px;
}
.pld-creator {
  float: left;
  font-size: 16px;
  margin: auto 20px;
}
.pld-creator a:hover {
  color: var(--color-theme);
}
.pld-createTime {
  font-size: 14px;
  color: #999;
}
.pld-tags {
  height: 50px;
  line-height: 50px;
}
.tag-item {
  padding-right: 10px;
}
.tag-item:hover {
  color: var(--color-theme);
}
/* mod-songlist-toolbar */
.mod-pld .mod-songlist-toolbar {
  position: absolute;
  bottom: 0;
}
.mod-songlist-toolbar .mod-btn,
.mod-songlist-toolbar .mod-btn-blue {
  min-width: 122px;
  text-align: center;
}
.mod-songlist-toolbar .iconfont {
  width: 13px;
  height: 16px;
}
/* pld-left */
.pld-left {
  float: left;
  width: 850px;
}
/* 歌曲列表头 */
.songlist-message {
  height: 50px;
  line-height: 50px;
}
.songlist-message h3 {
  float: left;
  font-weight: 400;
  margin-right: 15px;
}
.songlist-message .songlist-num {
  font-size: 14px;
  color: #999;
}
.songlist-message .songlist-play {
  float: right;
  margin-right: 20px;
  font-size: 14px;
}
.songlist-message .songlist-play .playcount {
  color: var(--color-theme);
}

.songlist-el-pagination,
.comment-el-pagination {
  position: relative;
  text-align: center;
  margin: 20px auto;
}
/* pld-relative */
.pld-relative {
  float: right;
  width: 300px;
  overflow: hidden;
}
.pld-relative-box {
  position: relative;
  width: 100%;
  overflow: hidden;
}
.pld-relative-hd {
  margin-bottom: 10px;
  font-weight: 400;
  height: 36px;
  line-height: 36px;
}
.pld-description {
  width: 100%;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  font-size: 14px;
  line-height: 20px;
  margin-bottom: 20px;
  white-space: pre-wrap;
  white-space: -moz-pre-wrap;
  white-space: -pre-wrap;
  white-space: -o-pre-wrap;
  /* *word-wrap: break-word; */
  /* *white-space: normal; */
}
.pld-description.limit {
  max-height: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pld-description-height {
  float: right;
  font-size: 14px;
  color: var(--color-theme);
  cursor: pointer;
  margin-bottom: 20px;
}
.pld-collector {
  width: 100%;
  margin-bottom: 20px;
}
.pld-collector-item {
  float: left;
  width: 65px;
  height: 65px;
  margin-right: 10px;
  margin-bottom: 10px;
}
.pld-collector-item a {
  display: block;
  width: 65px;
  height: 65px;
}
.pld-collector-item img {
  width: 65px;
  height: 65px;
}
.not-logged {
  position: relative;
  width: 1200px;
  height: 455px;
  margin: 0 auto;
  overflow: hidden;
}
.not-logged span {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}
.not-logged .not-content {
  top: 320px;
  text-align: center;
}
.noSong {
  position: relative;
  height: 250px;
  width: 100%;
}
.noSong .hd {
  margin-top: 100px;
  height: 50px;
  line-height: 50px;
  font-weight: 700;
  font-size: 24px;
  text-align: center;
}
.noSong .m {
  height: 30px;
  line-height: 30px;
  font-size: 14px;
  color: #999;
  text-align: center;
}
.noSong .iconfont,
.noSong a {
  color: var(--color-theme);
}
</style>